﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command;
using B.S.DataBase.MyRead.API.Appicant.Command.RoleMenus;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.RoleMenus
{
    public class GetRoleCommendHandlers : IRequestHandler<GetRoleCommends, APIResult<List<getRolesDto>>>
    {
        private readonly IBaseRepository<Role> roleRep;
        private readonly IMapper mapper;

        public GetRoleCommendHandlers(IBaseRepository<Role> roleRep, IMapper mapper)
        {
            this.roleRep = roleRep;
            this.mapper = mapper;
        }



        /// <summary>
        /// 获取角色————角色分配权限
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<getRolesDto>>> Handle(GetRoleCommends request, CancellationToken cancellationToken)
        {
            APIResult<List<getRolesDto>> result = new APIResult<List<getRolesDto>>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();

            var list = roleRep.GetAll().Where(x => x.IsDel == false);
            var data=mapper.Map<List<getRolesDto>>(list.ToList());
            result.data = data;
            return  Task.FromResult(result);
        }
    }
}
